import urllib
import ConfigParser
import logging
import sqlite3
from gcalendar import *

# to be removed 
from pprint import pprint


CONFIG_FILE = 'ACcfg.ini'
CALENDAR_FILE = 'calendar'
CALENDAR_DB = 'calendar.db'
#logging.basicConfig(filename='admin-calendar.log',level=logging.DEBUG)
#logging.basicConfig(level=logging.DEBUG)


def get_config(config_file):
    confpars = ConfigParser.ConfigParser()
    fd = open(config_file)
    confpars.readfp(fd)
    global config
    config["calendar_url"] = confpars.get("MAIN","calendar_url")
    fd.close()

def callendar2db(calendar_file, calendar_db):
    #dbconn = sqlite3.connect(calendar_db)
    event_tab = parse_ical(CALENDAR_FILE)
    pprint (vars(event_tab[0]))
    pprint (vars(event_tab[1]))
    pprint (vars(event_tab[2]))
       
    

logging.info('Parsing configuration file: ' + CONFIG_FILE)	
config = {}
get_config(CONFIG_FILE)
#print config
logging.info('Download calendar from: ' + config["calendar_url"]) 
urllib.urlretrieve(config["calendar_url"], CALENDAR_FILE)
logging.info('Parsing calendar file to database: calendar file=' + CALENDAR_FILE + ' database=' + CALENDAR_DB)	
callendar2db(CALENDAR_FILE, CALENDAR_DB)
